﻿using UnityEngine;
using System.Collections;
namespace Room
{
    public class RCAISelf : RCAIActor
    {
        private bool pressing=false;
        private bool stoped = true;

        private Vector3 moveDirection = Vector3.zero;

        public int killNum;

        private bool autoPathFinding;
        public bool AutoPathFinding
        {
            get
            {
                return autoPathFinding;
            }
        }
        public RoomCharactor autoClickCharactor;
        private CompleteCallback arrivedCallback;


        public RCAISelf(RoomCharactor owner)
            :base(owner)
        {
            type = AIType.Self;
        }

        public override void SetKillNum(int killNum)
        {
            this.killNum = killNum;
            var data = Dict.Blo.DictKillerBlo.GetKillerData(killNum);
            var color = new Color(data.colorR / 255f, data.colorG / 255f, data.colorB / 255f);
            if(color.Equals(Color.white)) 
                owner.ui.SetColor(new Color((float)0x79 / (float)0xff, (float)0xed / (float)0xff, (float)0x1c / (float)0xff));
            else
                owner.ui.SetColor(color);
            if (data.jailFlag==1)
            {
                StaticData.isPlayerNeedJail = true;
            }
            if (killNum == 0)
                StaticData.isPlayerNeedJail = false;
        }

        public override int GetKillNum()
        {
            return killNum;
        }

        public override void Tick()
        {
            base.Tick();

            pressing = pressing && TouchKit.GetEnabled();
            if (pressing && !(moveDirection.x==0f && moveDirection.y==0f) )
            {
                //if(Time.time-lastMoveTime>0.1f)
                //{
                //    lastMoveTime = Time.time;
                Vector3 to = owner.transform.position;
                to += moveDirection;

                owner.MoveTo(to);

                autoPathFinding = false;
                autoClickCharactor = null;
                arrivedCallback = null;

                if (autoClickCharactor != null)
                {
                    Debug.Log("self move to " + to + " id " + autoClickCharactor.name);
                }
                
                    stoped = false;
                //}
            }
            else
            {
                //Debug.Log("self stop to " + to + " id " + autoClickCharactor.name);
                if (!stoped)
                {
                    Vector3 to = owner.transform.position;
                    to += moveDirection;

                    owner.StopTo(owner.transform.position);
                    stoped = true;
                }
            }
            if (autoPathFinding)
            {
                if(!owner.IsMoving())
                {
                    if(autoClickCharactor!=null)
                    {
                        autoClickCharactor.Click();
                    }
                    
                    if(arrivedCallback!=null)
                    {
                        arrivedCallback();
                    }

                    autoPathFinding = false;
                    arrivedCallback = null;
                    autoClickCharactor = null;
                }
            }

            if (owner.IsMoving())
            {
                var ai = owner.ai as RCAIActor;
                if (ai.curState == PlayerAIState.Normal)
                {
                    if (owner.lod.IsPlayingAnim("stand"))
                    {
                        owner.PlayAnim("run");
                    }
                }
            }
            else
            {
                var ai = owner.ai as RCAIActor;
                if (ai.curState == PlayerAIState.Normal)
                {
                    if (owner.lod.IsPlayingAnim("run") || !owner.lod.isPlayingAnim)
                    {
                        owner.PlayAnim("stand");
                    }
                }
            }
        }

        public override void DoResume()
        {
            TouchKit.On_MoveStart = this.OnJoystickMoveStart;
            TouchKit.On_MoveEnd = this.OnJoystickMoveEnd;
            TouchKit.On_Move = this.OnJoystickMove;

            owner.charactorManager.room.uiManager.onCharactorClickCallback += OnAnyCharactorClick;

            //刷新主角英雄、装备

            var heroId = StaticData.playerData.GetBattleCamp(1).hero.heroId;

            string weaponId = "";
            if (StaticData.playerData.GetBattleCamp(1).equipments[0] != null)
                weaponId = StaticData.playerData.GetBattleCamp(1).equipments[0].equipId;


            var quest = Story.StoryManager.Instance.GetCurQuest();
            //当前有正在进行的任务且任务要求地图里跑路的是主角
            if (quest != null && quest.forceMain == 1)
            {
                var uiHero = StaticData.playerData.GetUIHero(StaticData.playerData.heroWarUID);
                heroId = uiHero.heroId;
                for(int i=1;i<5;i++)
                {
                    var campHero = StaticData.playerData.GetBattleCamp(i).hero;
                    if (campHero!=null && campHero.guid.Equals(StaticData.playerData.heroWarUID))
                    {
                        var campWeapon = StaticData.playerData.GetBattleCamp(i).equipments[0];
                        if (campWeapon != null)
                            weaponId = campWeapon.equipId;
                    }
                }
            }

            RCLODPlayer lod = owner.lod as RCLODPlayer;
            lod.Refresh(heroId, weaponId);

            RCUIPlayer ui = owner.ui as RCUIPlayer;
            if(ui!=null)
            {
                ui.UpdateNamePlace();
            }
            ui.SetTitle(StaticData.playerData.curTitle);
        }
        public override void DoPause()
        {
            TouchKit.On_MoveStart = null;
            TouchKit.On_MoveEnd = null;
            TouchKit.On_Move = null;

            owner.charactorManager.room.uiManager.onCharactorClickCallback -= OnAnyCharactorClick;

            stoped = true;
            pressing = false;
            StopAutoGo();
        }
        private void OnJoystickMove(Vector2 joystickAxis)
        {

            //if(!joystickAxis.Equals(joystickAxis))
            //{

            var ai = owner.ai as RCAIActor;
            if (ai.curState == PlayerAIState.Normal)
            {
                moveDirection = new Vector3(joystickAxis.x, 0, joystickAxis.y).normalized * 0.3f;
                Quaternion que = Quaternion.Euler(StaticData.cameraRotate);
                moveDirection = que * moveDirection;
            }

            //lastMoveTime = 0f;
            //}

        }
        private void OnJoystickMoveStart(/*MovingJoystick move*/)
        {
            pressing = true;
        }
        private void OnJoystickMoveEnd(/*MovingJoystick move*/)
        {
            pressing = false;
            moveDirection = Vector3.zero;
        }

        private void OnAnyCharactorClick(RoomCharactor charactor)
        {
            if (charactor.type == CharactorType.NormalNPC ||
                charactor.type == CharactorType.StoryNPC ||
                charactor.type == CharactorType.Area ||
                charactor.type == CharactorType.Box ||
                charactor.type == CharactorType.Mine )
            {
                if ((charactor.transform.position - owner.transform.position).magnitude < charactor.ui.ClickRange)
                {
                    //charactor.OnClick();
                }
                else
                {
                    GoToAndClick(charactor,charactor.ui.ClickRange-0.3f);
                }
            }
            
            else if(charactor.type == CharactorType.OtherPlayer ||
                charactor.type == CharactorType.ServerMonster||
                charactor.type == CharactorType.FieldBoss)
            {
                GoToAndClick(charactor);
            }
        }

        public void GoToAndClick(RoomCharactor charactor,float range=0.5f)
        {
            Vector3 target = Vector3.MoveTowards(charactor.transform.position, owner.transform.position, range);
             
            owner.StopTo(target);
            autoPathFinding = true;
            autoClickCharactor = charactor;
            arrivedCallback = null;
        }
        
        public void AutoGoto(Vector3 target, CompleteCallback callback,float range=0.8f)
        {
            Vector3 target0 = Vector3.MoveTowards(target, owner.transform.position, range);
            owner.StopTo(target0);
            autoPathFinding = true;
            arrivedCallback = callback;
            autoClickCharactor = null;
        }

        public void StopAutoGo()
        {
            owner.StopMove();
            autoPathFinding = false;
            autoClickCharactor = null;
            arrivedCallback = null;
        }
    }

    public delegate void CompleteCallback();
}

